﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ApiPlugin
{
    /// <summary>
    /// 数据格式统一
    /// </summary>
    public class UnifyResultFilter : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is ObjectResult)
            {
                var objectResult = context.Result as ObjectResult;
                context.Result = new ObjectResult(new { Code = 200, Msg = "", Result = objectResult?.Value });
            }
            else if (context.Result is EmptyResult)
            {
                context.Result = new ObjectResult(new { Code = 200 , Msg = "" });
            }
            else if (context.Result is ContentResult)
            {
                context.Result = new ObjectResult(new { Code = 200, Msg = "", Result = (context.Result as ContentResult).Content });
            }
            else if (context.Result is StatusCodeResult)
            {
                context.Result = new ObjectResult(new { Code = (context.Result as StatusCodeResult).StatusCode, Msg = "" });
            }
        }
    }
}
